package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.exception.BusinessException;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    private EmployeeMapper employeeMapper;

    @Autowired
    public void setEmployeeMapper(EmployeeMapper employeeMapper) {
        this.employeeMapper = employeeMapper;
    }

    @Override
    public void insert(Employee employee, Long[] ids) {
        employeeMapper.insert(employee);
        if (ids != null){
            for (Long rid : ids){
                employeeMapper.insertReletion(rid,employee.getId());
            }
        }
    }

    @Override
    public void delete(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
        employeeMapper.deleteRelation(id);
    }

    @Override
    public void update(Employee employee, Long[] ids) {
        employeeMapper.updateByPrimaryKey(employee);
        employeeMapper.deleteRelation(employee.getId());
        if (ids != null){
            for (Long rid : ids){
                employeeMapper.insertReletion(rid,employee.getId());
            }
        }
    }

    @Override
    public Employee selectId(Long id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Employee> selectAll() {
        return employeeMapper.selectAll();
    }

    @Override
    public PageInfo<Employee> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> list = employeeMapper.queryList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<Role> selectRolesByEmpId(Long id) {
        return employeeMapper.selectRolesByEmpId(id);
    }

    @Override
    public Employee checkUser(String username, String password) {
        if (StringUtils.isEmpty(username)){
            throw new BusinessException("账号不能为空");
        }else if (StringUtils.isEmpty(password)){
            throw new BusinessException("密码不能为空");
        }
        Employee employee = employeeMapper.checkByUsername(username);
        if (employee == null || !employee.getPassword().equals(password)){
            throw new BusinessException("账号或密码 有误");
        }
        return employee;
    }

    @Override
    public List<Permission> selectPermissionByEmpId(Long empId) {
        return employeeMapper.selectPermissionByEmpId(empId);
    }

    @Override
    public void exportXls(ServletOutputStream outputStream) throws IOException {
        //定义工作表
        Workbook wb = new HSSFWorkbook();
        //定义工作页
        Sheet sheet1 = wb.createSheet("员工信息");
        //定义行
        Row row = sheet1.createRow(0);
        //定义单元格
        row.createCell(0).setCellValue("员工编号");
        row.createCell(1).setCellValue("员工姓名");
        row.createCell(2).setCellValue("员工邮箱");
        List<Employee> employees = employeeMapper.selectAll();
        for (int i = 0; i <  employees.size() ; i++){
            Employee emp = employees.get(i);
            Row row1 = sheet1.createRow(i+1);
            row1.createCell(0).setCellValue(emp.getId());
            row1.createCell(1).setCellValue(emp.getName());
            row1.createCell(2).setCellValue(emp.getEmail());
        }
        wb.write(outputStream);
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {
        Workbook wb = new HSSFWorkbook(file.getInputStream());
        Sheet sheet = wb.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
            Row row1 = sheet.getRow(i);
            Employee employee = new Employee();
            employee.setName(row1.getCell(0).getStringCellValue());
            employee.setUsername(row1.getCell(1).getStringCellValue());
            Double numericCellValue = row1.getCell(2).getNumericCellValue();
            employee.setAge(numericCellValue.intValue());
            employee.setEmail(row1.getCell(3).getStringCellValue());
            employee.setAdmin(false);
            employeeMapper.insert(employee);
        }
    }
}
